package com.gitee.zycra.future.job.web.controller;

import com.gitee.zycra.future.job.common.bo.CommonResult;
import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.param.ManualTriggerParam;
import com.gitee.zycra.future.job.common.param.PageQueryRunningParam;
import com.gitee.zycra.future.job.common.vo.JobRunningVO;
import com.gitee.zycra.future.job.service.JobRunningService;
import com.gitee.zycra.future.job.web.annotation.AuthCheck;
import com.gitee.zycra.jdbc.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zycra
 */
@Slf4j
@RestController
@RequestMapping("/running")
public class JobRunningController {

    @Autowired
    private JobRunningService jobRunningService;

    /**
     * 查询
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    @AuthCheck({"4", "5", "6", "7"})
    @PostMapping("/pageList")
    public CommonResult<PageResult<JobRunningVO>> pageList(@RequestBody @Validated PageQuery<PageQueryRunningParam> pageQuery) {
        PageResult<JobRunningVO> pageResult = jobRunningService.selectJobRunning(pageQuery);
        return CommonResult.success(pageResult);
    }

    /**
     * 手动触发任务
     *
     * @param manualTriggerParam 调度参数
     * @return 执行结果
     */
    @AuthCheck({"5", "7"})
    @PostMapping("/manualTrigger")
    public CommonResult<String> manualTrigger(@RequestBody @Validated ManualTriggerParam manualTriggerParam) {
        if (manualTriggerParam.getParam() == null || manualTriggerParam.getParam().isBlank()) {
            manualTriggerParam.setParam("{}");
        }
        return jobRunningService.manualTrigger(manualTriggerParam);
    }

    /**
     * 手动取消任务
     *
     * @param id 执行ID
     * @return 取消结果
     */
    @AuthCheck({"5", "7"})
    @PostMapping("/manualCancel")
    public CommonResult<String> manualCancel(@RequestParam("id") Long id) {
        return jobRunningService.manualCancel(id);
    }
}
